//
//  ASHUD.m
//  
//
//  Created by  on 2023/3/3.
//  Copyright © 2023 lishen. All rights reserved.
//

#import "ASHUD.h"

#ifndef dispatch_main_async_safe
#define dispatch_main_async_safe(block)\
    if (dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL) == dispatch_queue_get_label(dispatch_get_main_queue())) {\
        block();\
    } else {\
        dispatch_async(dispatch_get_main_queue(), block);\
    }
#endif
#define HudSizeFit375(num)  (((num) * [UIScreen mainScreen].bounds.size.width) / 375.f)
#define kRGBA(r, g, b, a) ([UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a])

@implementation ASHUD

/**
 显示FCloading图标到指定view上，隐藏方法则直接点用[FCHUD hideHUDForView:]
 
 @param view 指定view，如果view为nil，则直接添加到window
 @return MBProgressHUD
 */
+ (MBProgressHUD *)showLoadingWithView:(UIView *)view {
    return [self showLoadingWithView:view userInteractionEnabled:YES];
}

+ (MBProgressHUD *)showLoadingWithView:(UIView *)view offsetY:(CGFloat)offsetY {
    MBProgressHUD *hud = [[self class] showLoadingWithView:view];
    hud.offset = (CGPoint){0.f, offsetY};
    return hud;
}

/**
 显示FCloading图标到指定view上，隐藏方法则直接点用[FCHUD hideHUDForView:]
 
 @param view 指定view，如果view为nil，则直接添加到window
 @param userInteractionEnabled 蒙版是否可以点击
 @return MBProgressHUD
 */
+ (MBProgressHUD *)showLoadingWithView:(UIView *)view userInteractionEnabled:(BOOL)userInteractionEnabled {
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.userInteractionEnabled = userInteractionEnabled;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor clearColor];
    //    hud.defaultMotionEffectsEnabled = YES;
    UIImageView *imageView = [[UIImageView alloc] init];
    NSMutableArray<UIImage *> *array = [NSMutableArray array];
    for (int i = 1; i <= 7; i++) {
//        NSString *name = [NSString stringWithFormat:@"load00%02d@2x.png", i];
//        NSString *filePath = [bundle pathForResource:name ofType:nil inDirectory:@"FCFrameWork.bundle"];
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_loading%d", i]];

        [array addObject:image];
    }
    imageView.animationImages = array;
    [imageView startAnimating];
    hud.customView = imageView;
    hud.mode = MBProgressHUDModeCustomView;
    //    hud.removeFromSuperViewOnHide = YES;
    return hud;
}

/**
 显示FCloading图标到window上，隐藏方法则直接点用[FCHUD hideHUDForView:]
 
 @return MBProgressHUD
 */
+ (MBProgressHUD *)showLoading {
    return [self showLoadingWithView:[UIApplication sharedApplication].keyWindow];
}

/**
 菊花转loading到指定view，需要手动隐藏
 
 @param message 信息内容
 @param view 指定view
 @return MBProgressHUD
 */
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view {
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.bezelView.style = MBProgressHUDBackgroundStyleBlur;
    hud.bezelView.backgroundColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:0.3];
    hud.removeFromSuperViewOnHide = YES;
    hud.defaultMotionEffectsEnabled = YES;
    [UIActivityIndicatorView appearance].activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    hud.label.text = message;
    return hud;
}

/**
 window上显示菊花转loading
 
 @param message 信息内容
 @return MBProgressHUD
 */
+ (MBProgressHUD *)showMessage:(NSString *)message {
    return [self showMessage:message toView:nil];
}

/**
 简单文字提示。tost，不自动消失
 
 @param text 信息内容
 */
+ (void)showNoDismissWithText:(NSString *)text {
    UIView *view = [UIApplication sharedApplication].keyWindow;
    view.hidden = NO;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    hud.label.numberOfLines = 0;
    hud.label.text = text;
    hud.label.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15];
    hud.contentColor = [UIColor whiteColor];
    hud.mode = MBProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
}

/**
 简单文字提示。tost，自动消失
 
 @param text 信息内容
 */
+ (void)showWithText:(NSString *)text {
    [self showWithText:text afterDelay:3.0];
}
+ (void)showWithText:(NSString *)text position:(HudPosition)position {
    [self showWithText:text afterDelay:3.0 position:position];
}
/**
 简单文字提示。tost，自动消失
 
 @param text 信息内容
 @param afterDelay 多久之后消失
 */
+ (void)showWithText:(NSString *)text afterDelay:(CGFloat)afterDelay {
    [[self class] showWithText:text afterDelay:afterDelay position:kHudPosition_Center];
}

+ (void)showWithText:(NSString *)text afterDelay:(CGFloat)afterDelay position:(HudPosition)position  {
    dispatch_main_async_safe(^{
        __block UIView *view = [UIApplication sharedApplication].keyWindow;
        if (view) {
            view.hidden = NO;
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
            hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
            hud.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
            hud.label.numberOfLines = 0;
    //        hud.userInteractionEnabled = YES;
            hud.userInteractionEnabled = NO;
            hud.label.text = text;
            hud.label.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15];
            hud.contentColor = [UIColor whiteColor];
            hud.mode = MBProgressHUDModeCustomView;
            hud.removeFromSuperViewOnHide = YES;
            [hud hideAnimated:YES afterDelay:afterDelay];
            CGFloat hudCenterY = hud.superview.frame.size.height / 2.f;
            if (position == kHudPosition_Bottom) {
                hud.offset = CGPointMake(0.f, hudCenterY - HudSizeFit375(120.f));
            } else if (position == kHudPosition_Top) {
                hud.offset = CGPointMake(0.f, HudSizeFit375(180.f) - hudCenterY);
            }
        }
    });
}

/**
 简单文字提示。tost，需要手动调用消息
 
 @param text 信息内容
 */
+ (void)showWithHUDText:(NSString *)text {
    UIView *view = [UIApplication sharedApplication].keyWindow;
    view.hidden = NO;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = text;
    hud.label.numberOfLines = 0;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    hud.contentColor = [UIColor whiteColor];
    hud.mode = MBProgressHUDModeCustomView;
}

/**
 文字提示加上图标，tost到指定view
 
 @param text 文字
 @param icon 图标
 @param view 指定view
 */
+ (void)show:(NSString *)text icon:(UIImage *)icon view:(UIView *)view {
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    view.hidden = NO;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    hud.contentColor = [UIColor whiteColor];
    hud.label.text = text;
    hud.label.numberOfLines = 0;
    hud.customView = [[UIImageView alloc] initWithImage:icon];
    hud.mode = MBProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:1.0];
}

/**
 提示错误信息导置顶指定view
 
 @param error 信息内容
 @param view 指定view
 */
+ (void)showError:(NSString *)error toView:(UIView *)view {
    NSBundle *bundle = [NSBundle bundleForClass:self.class];
    NSString *name = [NSString stringWithFormat:@"MBHUD_Error"];
//    NSString *filePath = [bundle pathForResource:name ofType:nil inDirectory:@"CommonBase.bundle"];
    UIImage *image = [self imageNamed:name];
    [self show:error icon:image view:view];
}

/**
 window上提示错误信息
 
 @param error 错误信息
 */
+ (void)showError:(NSString *)error {
    [self showError:error toView:nil];
}

/**
 提示成功信息
 
 @param success 信息内容
 @param view 要显示的view
 */
+ (void)showSuccess:(NSString *)success toView:(UIView *)view {
    NSBundle *bundle = [NSBundle bundleForClass:self.class];
    NSString *name = [NSString stringWithFormat:@"MBHUD_Success"];
//    NSString *filePath = [bundle pathForResource:name ofType:nil inDirectory:@"CommonBase.bundle"];
    UIImage *image = [self imageNamed:name];
    [self show:success icon:image view:view];
}

/**
 window上提示成功信息
 
 @param success 成功信息
 */
+ (void)showSuccess:(NSString *)success {
    [self showSuccess:success toView:nil];
}


/**
 隐藏制定view上的hud
 
 @param view 指定的view
 */
+ (void)hideHUDForView:(UIView *)view {
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    [MBProgressHUD hideHUDForView:view animated:YES];
}

/**
 隐藏window上的hud
 */
+ (void)hideHUD {
    [self hideHUDForView:nil];
}

+ (UIImage *)imageNamed:(NSString *)name {
    UIImage *image = nil;

    NSString *image_name = [NSString stringWithFormat:@"%@@2x.png", name];

    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];

    NSString *bundlePath = [NSString stringWithFormat:@"%@/%@",[resourcePath stringByAppendingPathComponent:@"CommonBase.bundle"],@"CommonBase.bundle"];

    NSString *image_path = [bundlePath stringByAppendingPathComponent:image_name];

    image = [[UIImage alloc] initWithContentsOfFile:image_path];

    return image;
}


+ (void)showASLoading{
    [self hideHUD];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.layer.backgroundColor = kRGBA(10, 10, 10, 0.8).CGColor;
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.label.text = @"loading";
    hud.label.textColor = [UIColor whiteColor];
    [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[[MBProgressHUD class]]].color = [UIColor whiteColor];
    hud.label.font = [UIFont systemFontOfSize:15];
    [hud hideAnimated:YES afterDelay:30];
}

@end
